package com.wildwolf.project.wildwolf.utils;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.widget.Toast;

import com.wildwolf.project.wildwolf.WildWolfService;
import com.wildwolf.project.wildwolf.R;
/**
 * 管理是否掉线
 * @author LEON
 *
 */
public class WildWolfBroadcastReceiver extends BroadcastReceiver {

	/** 掉线的Action. */
	public static final String WILDWOLF_CONNECTION_CLOSED = "WildwolfConnectionClosed";

	/**
	 * .
	 */
	public WildWolfBroadcastReceiver() {
	}
	/**
     * 
     */
	@Override
	public void onReceive(final Context context, final Intent intent) {
		String intentAction = intent.getAction();
		if (intentAction.equals(WILDWOLF_CONNECTION_CLOSED)) {
			CharSequence message = intent.getCharSequenceExtra("message");
			Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
			if (context instanceof Activity) {
				Activity act = (Activity) context;
				act.finish();
				// 服务将在Activity销毁的时候解除绑定
			}
		} else if (intentAction.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
			if (intent.getBooleanExtra(	ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
				Toast.makeText(
						context,
						context.getString(R.string.WildwolfBroadcastReceiverDisconnect),
						Toast.LENGTH_SHORT).show();
				context.stopService(new Intent(context, WildWolfService.class));
			}
		}
	}
}
